package com.wenx.ftpcamel;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

import javax.jms.ConnectionFactory;

/**
 * 本地路径下文件传送经过activemq
 */
public class FileDemo4 {
    private static String user = "admin";
    private static String password = "admin";
    //填写AMQ的IP
    private static String url = "tcp://localhost:61616";
    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();
        //创建AMQ的连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(connectionFactory);
        //使用连接池连接
        activeMQComponent.setUsePooledConnection(true);
        //事务
        activeMQComponent.setTransacted(true);
        //客户端确认
        activeMQComponent.setAcknowledgementMode(3);

        camelContext.addComponent("activemq", activeMQComponent);
        System.out.println(url + " " + user + " " + password);
        camelContext.start();

        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("activemq:topic:VirtualTopic.test")
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                System.out.println("*******************");
                                System.out.println("文件名 ： " + exchange.getIn().getHeader(Exchange.FILE_NAME));
                                System.out.println("绝对路径 ： " + exchange.getIn().getHeader("CamelFileAbsolutePath"));
                                System.out.println("最后修改时间 ： " + exchange.getIn().getHeader("CamelFileLastModified"));
                                System.out.println("文件大小： " + exchange.getIn().getHeader("CamelFileLength"));
                            }
                        })
                        .to("file:D://camelFile//fileC");
            }
        });
        synchronized (FileDemo4.class) {
            FileDemo4.class.wait();
        }
    }
}
